package controller.shop;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import model.GoodsSort;
import model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import service.IGoodsService;
import service.IGoodsSortService;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @aythor:侯仪澳
 * @data:2022/8/3
 */
@Controller
@RequestMapping("shop")
public class ShopController {
    @Autowired
    private IGoodsService goodsService;
    @Autowired
    private IGoodsSortService goodsSortService;

    @RequestMapping("shopPage")
    public ModelAndView mainPage(){
        //查询父级分类
        List<GoodsSort> list = goodsSortService.queryOneSort(null);
        //创建json数组
        JSONArray arr = new JSONArray();
        for (GoodsSort s: list) {
            //集合元素转json对象
            JSONObject ele = (JSONObject) JSON.toJSON(s);

            //查询子分类
            List<GoodsSort> kidList = goodsSortService.queryOneSort(new GoodsSort(s.getId()));
            JSONArray kidArr = (JSONArray) JSON.toJSON(kidList);
            ele.put("kidArr", kidArr);
            arr.add(ele);
        }

        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("arr", arr);
        modelAndView.setViewName("shopping/mall_main");
        return modelAndView;
    }
}
